/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 				</p>
 * <p>		    All right reserved. 		   			</p>
 * <p>项目名称 ：深圳公路信息资源整合及国省检日常化监管管理        				</p>
 * <p>创建者 ：hey										</p>
 * <p>描   述  ：路政管理 - 执法监督 - 举报渠道畅通			 			</p>
 * <p>最后修改 ：$: 2017年9月4日-上午10:35:21 v 1.0.0	 hey   $   </p>
 * 
*/
package com.pengtu.gsj.service.impl.check.lzgl.overloadMan;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.lzgl.GovernSpeedDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lzgl.GovernSpeed;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lzgl.overloadMan.OverloadMechService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;


@Service
@Transactional
public class OverloadMechServiceImpl implements OverloadMechService {
	
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	private GovernSpeedDao governSpeedDao;
	
	@Override
	public List<GovernSpeed> getGovernSpeedList(QueryFilter queryFilter){
		return governSpeedDao.find(queryFilter, "testYear", true);
	}
	
	@Override
	public Map<String, Object> getDevelopPlanmMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<GovernSpeed> lawList = new ArrayList<GovernSpeed>();
		List<Object> value = new ArrayList<Object>();
		List<List<String>> resultLists = new ArrayList<List<String>>();
		List<String> checkResult = Arrays.asList("0", "0", "0");
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		
		//每个区县治超有计划，有部署，有检查考核得分
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		//建立多部门联动执法得分
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		//建立区域协作机制得分
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		
		String governSpeedId = (String)jsonObject.get("governSpeedId");
		
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		
		double total = 0; 				//检查得分
		double percentage = 0; 			//系统完善度
		double tableFull  = (yearFull + yearPart + poitMark)*4.0;	//满分
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 1)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("testYear", startTime, MatchType.LE);
		queryFilter.addFilter("testYear", endTime, MatchType.GE);
		boolean flag = false;
		//三个临时变量，分别用于计算建立多部门协作机制得分，建立区域协作机制得分和文件得分
		double muTemp = 0;
		double reTemp = 0;
		double liTemp = 0;
		if (StringUtils.isEmpty(governSpeedId)) {
			lawList = this.getGovernSpeedList(queryFilter);
		} else {
			value.add(governSpeedId);
			flag = true;
			List<String> listResult = null;
			String[] idArrs = governSpeedId.split(",");
			for (int i = 0; i < idArrs.length; i++) {
				listResult = Arrays.asList("0", "0", "0");
				queryFilter.addFilter("id", idArrs[i], MatchType.EQ);
				GovernSpeed governSpeed = governSpeedDao.get(idArrs[i]);
				
				total += "1".equals(governSpeed.getMultisectoralCollaboration()) ? yearPart : 0;
				muTemp += "1".equals(governSpeed.getMultisectoralCollaboration()) ? yearPart : 0;
				total += "1".equals(governSpeed.getRegionalCollaboration()) ? poitMark : 0;
				reTemp += "1".equals(governSpeed.getRegionalCollaboration()) ? poitMark : 0;
				for (Attachment attachment : list) {
					if (idArrs[i].equals(attachment.getPartId())) {
						if ("zcjz_gzjh".equals(attachment.getTag())) {
							listResult.set(0, "1");
						}
						if ("zcjz_gzbs".equals(attachment.getTag())) {
							listResult.set(1, "1");
						}
						if ("zcjz_gzjc".equals(attachment.getTag())) {
							listResult.set(2, "1");
						}
					}
				}
				//判断三个文件是否都有
				boolean checkYearList = true;
				for (int j = 0; j < listResult.size(); j++) {
					if (!"1".equals(listResult.get(j))) {
						checkYearList = false;
					}
				}
				total += checkYearList ? yearFull : 0;
				liTemp += checkYearList ? yearFull : 0;
				resultLists.add(listResult);
				lawList.add(governSpeed);
			}
		}
		checkResult.set(0, String.valueOf(liTemp));
		checkResult.set(1, String.valueOf(muTemp));
		checkResult.set(2, String.valueOf(reTemp));
		
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, value);
		map.put("percent", percentage);
		map.put("years", years);
		map.put("att", list);
		map.put("flag", flag);
		map.put("checkResult", checkResult);
		map.put("lawList", lawList);
		map.put("resultLists", resultLists);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
